package com.example.wnn;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Objects;

public class HttpGetWithCookies {
    public static void main(String[] args) {
        try {
            String url = "https://jyt-acth5-pre.app.centv.cn/account/api/get-account";
            URL obj = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) obj.openConnection();

            // 设置Cookies
            connection.setRequestProperty("Cookie", "account_session=08f822af080e2934e735c23e75503289");

            // 设置其他属性
            connection.setRequestMethod("GET");

            // 发送请求
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);

            // 读取响应内容
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            // 输出响应内容
            System.out.println(response.toString());

            JSONObject jsonObject = JSONUtil.parseObj(response);
            if (jsonObject.containsKey("status") && "ok".equals(jsonObject.get("status",String.class))){
                if (jsonObject.containsKey("data") && Objects.nonNull(jsonObject.get("data"))){
                    Map<String,Object> dataMap = jsonObject.get("data",Map.class);
                  System.out.println(MapUtil.getStr(dataMap,"phone"));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}